package com.ebring.shareEducation.service.impl;

import com.ebring.shareEducation.bean.*;
import com.ebring.shareEducation.bean.Currency;
import com.ebring.shareEducation.dao.CurrencyMapper;
import com.ebring.shareEducation.service.CurrencyService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Cola
 * Date  2018-12-08 09:48
 */

@Service
@Transactional
public class CurrencyServiceImpl implements CurrencyService {

    @Autowired
    CurrencyMapper currencyMapper;


    private static Logger logger = LoggerFactory.getLogger(CurrencyServiceImpl.class);


    // 查询所有的菜单
    @Override
    public RspMsg<Currency> selectTask() {
        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List<Currency> list=currencyMapper.selectAll();
        if (list.isEmpty() || list == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关任务");
            return rspMsg;
        }
        map.put("currencylist",list);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;
    }

    @Override
    public RspMsg<Currency> updateTask(Currency currency) {
        RspMsg rspMsg = new RspMsg();
        int i = currencyMapper.updateByPrimaryKeySelective(currency);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("修改失败");
            return rspMsg;
        }
        rspMsg.setOK();
        return rspMsg;

    }

    @Override
    public RspMsg<Currency> deleteById(String currencyId) {
        RspMsg rspMsg = new RspMsg();
        int i = currencyMapper.deleteByPrimaryKey(currencyId);
        if (i == 0) {
            rspMsg.setError();
            rspMsg.setMsg("删除失败");
            return rspMsg;
        }

        rspMsg.setOK();
        return rspMsg;
    }

    @Override
    public RspMsg<List<Currency>> select(String cname, String time,String times) {
        Example example = new Example(Role.class);
        if ((time != null && !"".equals(time))&&(times != null && !"".equals(times))){
            example.createCriteria().andGreaterThanOrEqualTo("time", time).andLessThan("time", times);
        }
        if (cname != null && !"".equals(cname)){
            example.createCriteria().andLike("cname", "%"+cname+"%");
        }

        RspMsg rspMsg = new RspMsg();
        Map<String, Object> map = new LinkedHashMap<>();
        List currencyList = currencyMapper.selectByExample(example);
        if (currencyList.isEmpty() || currencyList == null) {
            rspMsg.setOK();
            rspMsg.setMsg("未找到相关用户");
            return rspMsg;
        }

        PageInfo pageInfo = new PageInfo<>(currencyList);

        // 总条数
        map.put("total", pageInfo.getTotal());
        // 每页显示条数
        map.put("curPage", pageInfo.getPageSize());
        // 总页数
        map.put("pages", pageInfo.getPages());
        // 当前页
        map.put("pageNum", pageInfo.getPageNum());
        map.put("currencyList", currencyList);
        rspMsg.setOK();
        rspMsg.setResult(map);
        return rspMsg;

    }

    // 增加虚拟币记录
    @Override
    public RspMsg addMoney(Currency currency) {
        RspMsg rspMsg = new RspMsg();
        currency.setId( UuidUtils.getNumberId(20));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        currency.setTime(simpleDateFormat.format(date));
        // 插入操作记录
        int i1 = currencyMapper.insertSelective(currency);
        if (i1 > 0) {
            rspMsg.setOK();
            rspMsg.setMsg("增加成功");
            logger.info("增加成功");
            return rspMsg;
        }


        rspMsg.setError();
        rspMsg.setMsg("增加记录失败");

        logger.info("增加记录失败");
        return rspMsg;
    }


}
